﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class RoomExamsDataMapper : AbstractDataMapper<RoomExamKey, RoomExam>
    {
        private IEntityMapper<int, Room> _roomsMapper;
        private IEntityMapper<int, Exam> _examsMapper;

        public void SetRoomsMapper(IEntityMapper<int, Room> mapper) { _roomsMapper = mapper; }
        public void SetExamsMapper(IEntityMapper<int, Exam> mapper) { _examsMapper = mapper; }
        protected override RoomExamKey GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return new RoomExamKey(sdr.GetInt32(1), sdr.GetInt32(0));
        }

        protected override string DoGetAllStatement()
        {
            return "select ExamID, RoomID from RoomExams";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ExamID = @ExamID and RoomID = @RoomID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, RoomExamKey key)
        {
            cmd.Parameters.AddWithValue("@ExamID", key.ExamKey);
            cmd.Parameters.AddWithValue("@RoomID", key.RoomKey);
        }

        protected override RoomExam DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            RoomExamKey key = GetKeyFrom(sdr);
            ConcreteValueHolder<int, Room> room = new ConcreteValueHolder<int,Room>(key.RoomKey, _roomsMapper);
            ConcreteValueHolder<int, Exam> exam = new ConcreteValueHolder<int,Exam>(key.ExamKey, _examsMapper);
            return new RoomExam(key, room, exam);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, RoomExam t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "insert into RoomExams (ExamID, RoomID) values (@ExamID, @RoomID)";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, RoomExam t)
        {
            cmd.Parameters.Add("@ExamID", SqlDbType.Int).Value = t.Exam.ID;
            cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = t.Room.ID;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from RoomExams where ExamID = @ExamId and RoomID = @RoomID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, RoomExam t)
        {
            cmd.Parameters.Add("@ExamID", SqlDbType.Int).Value = t.Exam.ID;
            cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = t.Room.ID;
        }

        protected override void FinishUpdateObject(RoomExam t)
        {
            
        }

        protected override void FinishNewObject(RoomExam t, object id)
        {
            t.ID = new RoomExamKey(t.Room.ID, t.Exam.ID);
        }

        protected override void FinishDeleteObject(RoomExam t)
        {
            
        }
    }
}
